 /* 
 * File:   cPhysicalObject.cpp
 * Author: josep
 * 
 * Created on 14 / desembre / 2010, 03:05
 */

#include "cPhysicalObject.h"
#include "Globals.h"

cPhysicalObject::cPhysicalObject() {
    FrictionFactor=0;
    for(int i=0; i<2; i++){
        AccelerationUnit[i]=DEF_ACCELERATION_UNIT;
        MaxForceFactor[i]=DEF_MAX_FORCE_FACTOR;
        forceFactor[i]=0;
    }
    for(int i=0; i<4; i++){
        SurfaceType[i]=0;
    }
}

cPhysicalObject::cPhysicalObject(const cPhysicalObject& orig) {
    FrictionFactor=orig.FrictionFactor;
    AccelerationUnit[X]=orig.AccelerationUnit[X];
    AccelerationUnit[Y]=orig.AccelerationUnit[Y];
    MaxForceFactor[X]=orig.MaxForceFactor[X];
    MaxForceFactor[Y]=orig.MaxForceFactor[Y];
    forceFactor[X]=orig.forceFactor[X];
    forceFactor[Y]=orig.forceFactor[Y];
    for(int i=0; i<4; i++){
        SurfaceType[i]=orig.SurfaceType[i];
    }
}

cPhysicalObject::~cPhysicalObject() {
}


void cPhysicalObject::NextStep(int time){
	IncSpeedX(forceFactor[X]*AccelerationUnit[X]*time);
	IncSpeedY(forceFactor[Y]*AccelerationUnit[Y]*time);
        forceFactor[X]=0;
        forceFactor[Y]=0;

	IncPosX(SpeedX*time);
	IncPosY(SpeedY*time);
}

bool cPhysicalObject::IsInTheAir() const {
    return inTheAir;
}

float cPhysicalObject::GetExactPosX(){
    return PosX;
}

float cPhysicalObject::GetExactPosY(){
    return PosY;
}
void cPhysicalObject::SetAccelerationUnit(int Dir, float Acceleration) {
    this->AccelerationUnit[Dir] = Acceleration;
}
float cPhysicalObject::GetAccelerationUnit(int Dir)  {
    return AccelerationUnit[Dir];
}
void cPhysicalObject::SetSurfaceType(int surfaceId, int SurfaceType) {
    this->SurfaceType[surfaceId] = SurfaceType;
}
int cPhysicalObject::GetSurfaceType(int surfaceId)  {
    return SurfaceType[surfaceId];
}

const int* cPhysicalObject::GetSurfaceType()  {
    return SurfaceType;
}

void cPhysicalObject::SetMaxForceFactor(int Dir, float MaxForceFactor) {
    this->MaxForceFactor[Dir] = MaxForceFactor;
}
float cPhysicalObject::GetMaxForceFactor(int Dir) const {
    return MaxForceFactor[Dir];
}
void cPhysicalObject::IncForceFactor(int Dir, float ff) {
    this->forceFactor[Dir] += ff;
    if(MaxForceFactor[Dir]>0){
	if(forceFactor[Dir]< 0 && forceFactor[Dir]< -MaxForceFactor[Dir]){
		this->forceFactor[Dir] = -MaxForceFactor[Dir];
	}
	if(forceFactor[Dir]>0 && forceFactor[Dir]>MaxForceFactor[Dir]){
		forceFactor[Dir]=MaxForceFactor[Dir];
	}
    }

}

void cPhysicalObject::SetForceFactor(int Dir, float ff){
    this->forceFactor[Dir] = ff;
    if(MaxForceFactor[Dir]>0){
	if(forceFactor[Dir]< 0 && forceFactor[Dir]< -MaxForceFactor[Dir]){
		this->forceFactor[Dir] = -MaxForceFactor[Dir];
	}
	if(forceFactor[Dir]>0 && forceFactor[Dir]>MaxForceFactor[Dir]){
		forceFactor[Dir]=MaxForceFactor[Dir];
	}
    }
}


float cPhysicalObject::GetForceFactor(int Dir) const {
    return forceFactor[Dir];
}

void cPhysicalObject::IncPosX(float inc){
    PosX+=inc;
}

void cPhysicalObject::IncPosY(float inc){
    PosY+=inc;
}

void cPhysicalObject::IncSpeedX(float inc){
    SpeedX+=inc;
}

void cPhysicalObject::IncSpeedY(float inc){
    SpeedY+=inc;
}

void cPhysicalObject::SetFrictionFactor(float FrictionFactor) {
    this->FrictionFactor = FrictionFactor;
}

float cPhysicalObject::GetFrictionFactor() const {
    return FrictionFactor;
}

void cPhysicalObject::StopX(int Pos){
    SetPosX(Pos);
    SetSpeedX(0);
}

void cPhysicalObject::StopY(int Pos){
    SetPosY(Pos);
    SetSpeedY(0);
}

void cPhysicalObject::Stop(int PosX, int PosY){
    StopX(PosX);
    StopY(PosY);
}

/*PROTECTED*/
void cPhysicalObject::SetInTheAir(bool inTheAir) {
    this->inTheAir = inTheAir;
}

void cPhysicalObject::Notify(cPhysicalReaction *reaction){}


